package com.android.vgongyi.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.android.vgongyi.R;
import com.android.vgongyi.common.CommentAdapter;
import com.android.vgongyi.entity.CommentEntity;
import com.android.vgongyi.mapper.CommentRequest;
import com.android.vgongyi.net.HttpConnector;
import com.android.vgongyi.parser.CommentParser;
import com.android.vgongyi.utils.SystemUtil;

public class CheckCommentActivity extends BaseActivity {
	private ListView commentListView;
	private CommentAdapter commentAdapter;
	private List<CommentEntity> entities = new ArrayList<CommentEntity>();
	private String itemId;
	private int commentSum;
	private int pageMaxSize;
	private LoadCommentData load;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.check_comments);
		
		final Intent intent = getIntent();
		this.itemId     = intent.getStringExtra("itemId");
		this.commentSum = Integer.valueOf(intent.getStringExtra("commentSum"));
		
		
        setCommonTitle(this, R.string.check_comments_check);
        
        Button backBtn  = (Button) this.findViewById(R.id.Common_back);
        Button replyBtn = (Button) this.findViewById(R.id.Common_refresh);
        commentListView = (ListView) this.findViewById(R.id.commentlist);
        backBtn.setVisibility(View.VISIBLE);
        replyBtn.setText(R.string.check_comments_reply);
        replyBtn.setVisibility(View.GONE);
        
        replyBtn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Intent i = new Intent();
				i.putExtra("weiboID", intent.getStringExtra("weiboID"));
				i.putExtra("weiboPlatform", intent.getStringExtra("weiboPlatform"));
				i.setClass(CheckCommentActivity.this,ReplyActivity.class);
				startActivity(i);
			}
		});
        
        backBtn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				CheckCommentActivity.this.finish();
			}
		});
	}
	

	@Override
	protected void onResume() {
		load = new LoadCommentData(this,checkPage());
		load.execute(itemId);
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		if(load!=null){
			load.cancel(true);
		}
		super.onPause();
	}

	public int checkPage(){
		pageMaxSize = commentSum / 10;
		if(commentSum % 10 > 0){
			pageMaxSize++;
		}
		return pageMaxSize;
	}

	private class LoadCommentData extends AsyncTask<String, String, String>{
		private Context context;
		private int pageSize; 
		LoadCommentData(Context context ,int pageSize){
			this.context = context;
			this.pageSize = pageSize;
		}
		@Override
		protected String doInBackground(String... params) {
			if(!SystemUtil.checkNetwork(context)){
				return null;
			}
			entities.clear();
			for(int i=0;i<pageSize;i++){
				CommentRequest commentRq = new CommentRequest(params[0], String.valueOf(i));
		        HttpConnector http = new HttpConnector(commentRq);
		        CommentParser commentParser = new CommentParser();
		        entities.addAll(commentParser.executeToObject(http.getURLResponse(), getApplicationContext()));
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			if(entities!=null){
				commentAdapter =new CommentAdapter(CheckCommentActivity.this, entities, commentListView);
		        commentListView.setAdapter(commentAdapter);
			}else{
				Toast.makeText(getApplicationContext(), R.string.check_comments_no_comments, Toast.LENGTH_SHORT).show();
			}
			super.onPostExecute(result);
		}
		
	}
}
